/*
 * ULSF Sagittarius IDA
 * Copyright (C) 2012, John Pritchard, all rights reserved.
 * 
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */
package sagittarius.design;

import fv3.math.Vector;

public abstract class Arrays {

    public final static float[][] Add(float[][] list, float[] item){
        if (null == item)
            return list;
        else if (null == list)
            return new float[][]{item};
        else {
            int len = list.length;
            float[][] copier = new float[len+1][];
            System.arraycopy(list,0,copier,0,len);
            copier[len] = item;
            return copier;
        }
    }
    public final static float[][][] Add(float[][][] list, float[][] item){
        if (null == item)
            return list;
        else if (null == list)
            return new float[][][]{item};
        else {
            int len = list.length;
            float[][][] copier = new float[len+1][][];
            System.arraycopy(list,0,copier,0,len);
            copier[len] = item;
            return copier;
        }
    }
    public final static float[] MinMax(float[] list){
        float max = Float.MIN_VALUE;
        float min = Float.MAX_VALUE;

        if (null != list){
            for (float value: list){
                max = Math.max(max,value);
                min = Math.min(min,value);
            }
        }
        return new float[]{
            min, max
        };
    }
    public final static float MinMaxAbsMax(float[] list){
        float[] minmax = MinMax(list);
        float a = Math.abs(minmax[0]);
        float b = Math.abs(minmax[1]);
        return Math.max(a,b);
    }
    public final static float MinMaxAbsMin(float[] list){
        float[] minmax = MinMax(list);
        float a = Math.abs(minmax[0]);
        float b = Math.abs(minmax[1]);
        return Math.min(a,b);
    }
    public final static float Max(float[] list){
        float max = Float.MIN_VALUE;

        if (null != list){
            for (float value: list){
                max = Math.max(max,value);
            }
        }
        return max;
    }
    public final static float Min(float[] list){
        float min = Float.MAX_VALUE;

        if (null != list){
            for (float value: list){
                min = Math.min(min,value);
            }
        }
        return min;
    }
    public final static void Scale(float[] vertices, float s){
        for (int p = 0, z = vertices.length; p < z; ){

            vertices[p++] *= s;
            vertices[p++] *= s;
            vertices[p++] *= s;

            p += 3;

            vertices[p++] *= s;
            vertices[p++] *= s;
            vertices[p++] *= s;

            p += 3;

            vertices[p++] *= s;
            vertices[p++] *= s;
            vertices[p++] *= s;

            p += 3;
        }
    }
    public final static void FillNormals(float[] vertices){

        for (int p = 0, z = vertices.length; p < z; ){

            final Vector a = new Vector(vertices[p++],vertices[p++],vertices[p++]);

            final int na = p;

            p += 3;

            final Vector b = new Vector(vertices[p++],vertices[p++],vertices[p++]);

            final int nb = p;

            p += 3;

            final Vector c = new Vector(vertices[p++],vertices[p++],vertices[p++]);

            final int nc = p;

            p += 3;

            final Vector normal = c.normal(b,a);

            normal.copyTo(vertices,na);
            normal.copyTo(vertices,nb);
            normal.copyTo(vertices,nc);
        }
    }
    public final static float Zero(float value){
        if (-0.0f == value)
            return 0.0f;
        else
            return value;
    }
}
